package cn.lg.soar.general.controller;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.api.enums.ThumbnailSizeEnum;
import cn.lg.soar.general.service.FileAccessService;
import com.amazonaws.services.s3.model.S3Object;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "文件访问控制")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/file")
public class FileAccessController {

    @Autowired
    private FileAccessService service;

    @ApiOperation("无需登录的临时路径")
    @GetMapping("anon/{sign}/{bucketName}/{fileInfo}")
    public void anonTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        responseFile(service.temporaryUrlAccess(sign, bucketName, fileInfo, size), res, null);
    }

    @ApiOperation("无需登录的临时路径")
    @GetMapping("anon/{sign}/{bucketName}/{fileInfo}/{filename}")
    public void anonTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @PathVariable String filename,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        responseFile(service.temporaryUrlAccess(sign, bucketName, fileInfo, size), res, filename);
    }

    @ApiOperation("需要登录的临时路径")
    @GetMapping("auth/{sign}/{bucketName}/{fileInfo}")
    public void authTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        res.setCharacterEncoding("UTF-8");
        responseFile(service.temporaryUrlAccess(sign, bucketName, fileInfo, size), res, null);
    }

    @ApiOperation("需要登录的临时路径")
    @GetMapping("auth/{sign}/{bucketName}/{fileInfo}/{filename}")
    public void authTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @PathVariable String filename,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        responseFile(service.temporaryUrlAccess(sign, bucketName, fileInfo, size), res, filename);
    }

    @ApiOperation("固定访问路径")
    @GetMapping("fixed/{secret}/{fileId}")
    public void fixedSecret(
            @ApiParam @PathVariable String secret,
            @ApiParam @PathVariable long fileId,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        responseFile(service.fixedSecretAccess(secret, fileId, size), res, null);
    }

    @ApiOperation("固定访问路径")
    @GetMapping("fixed/{secret}/{fileId}/{filename}")
    public void fixedSecret(
            @ApiParam @PathVariable String secret,
            @ApiParam @PathVariable long fileId,
            @ApiParam @PathVariable String filename,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse res
    ) throws IOException {
        responseFile(service.fixedSecretAccess(secret, fileId, size), res, filename);
    }

    private void responseFile(S3Object s3Object, HttpServletResponse res, String filename) throws IOException {
        res.setCharacterEncoding("UTF-8");
        res.setContentType(s3Object.getObjectMetadata().getContentType() + "; charset=UTF-8");
        if (DataUtil.isValuable(filename)) {
            res.setHeader("Content-Disposition", "attachment;fileName=\"" + filename + "\"");
        }
        FileCopyUtils.copy(s3Object.getObjectContent(), res.getOutputStream());
    }
}
